'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class XSLTCheck

      ' WARNING: This Assumes we will be doing only one thread with processing!!!!
      Private Shared mStatusOutput As IStatusOutput
      Private Shared mXsltFileName As String

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal directiveStatus As IStatusDirective)
         Dim xsltCheck As CodeGenerationSupport.IXSLTCheckDirective
         Dim itemstatus As Utility.SourceControlBase.ItemStatus
         Dim outputFile As String
         Dim stream As IO.Stream = Nothing
         Dim xsltParams() As CodeGenerationSupport.XSLTParam
         Dim extObjects() As ExtObject
         Dim madeFile As Boolean
         xsltCheck = CType(node, CodeGenerationSupport.IXSLTCheckDirective)
         mXsltFileName = xsltCheck.XSLTFile.XSLTFileName
         outputFile = xsltCheck.SinglePass.OutputFile
         mStatusOutput = directiveStatus.AddStatusOutput( _
                  outputFile, _
                  FileTreatment.Unknown)
         ' The cast is a bit of a hack. The interface doesn't currently include this method
         extObjects = CType(xsltCheck.XSLTFile, CodeGenerationHarness.XsltFileSupportElement).ExtObjects
         xsltParams = CType(xsltCheck.XSLTFile, CodeGenerationHarness.XsltFileSupportElement).XSLTParams

         ' Setup the callback for logging
         Dim uBound As Int32
         If extObjects Is Nothing Then
            ReDim extObjects(0)
            uBound = 0
         Else
            uBound = extObjects.GetUpperBound(0) + 1
            ReDim Preserve extObjects(uBound)
         End If
         extObjects(uBound).NameSpaceURI = Singletons.nameSpaceCheckCalback
         extObjects(uBound).Value = Me.GetType
         Try
            If runUtility.ShouldRun(outputFile, _
                     xsltCheck.OutputRule.OutputGenType, _
                     xsltCheck.OutputRule.HashWhitespace, _
                     xsltCheck.OutputRule.OutputFileType, mStatusOutput) Then
               Try
                  stream = CodeGenerationSupport.XSLTSupport.GenerateViaXSLT( _
                               xsltCheck.XSLTFile.XSLTFileName, _
                               xsltCheck.XSLTFile.InputFileName, _
                               xsltCheck.OutputRule.OutputFileType.ToString, _
                               extObjects, _
                               xsltParams)
                  madeFile = runUtility.MarkWriteAndClose(stream, _
                           xsltCheck.OutputRule.OutputFileType, _
                           xsltCheck.OutputRule.OutputGenType, _
                           xsltCheck.OutputRule.HashOutput, _
                           outputFile, mStatusOutput)
               Finally
                  Try
                     If stream IsNot Nothing Then
                        stream.Close()
                     End If
                  Catch
                     ' End no matter what
                  End Try
               End Try
            End If
            directiveStatus.CountCreated = 1
            directiveStatus.CountSelected = 1
            directiveStatus.CountOutput = 1
         Catch ex As System.Exception
            mStatusOutput.AddStatusException("CreateMetadata.Run", "Failure creating file", ex)
         End Try
         mStatusOutput.Status = mStatusOutput.ChildStatus
      End Sub

      Public Function LogWarning( _
                  ByVal msg As String) _
                  As String
         ' Log the output and return the string to also log in the XML file
         mStatusOutput.AddStatusWarning(mXsltFileName, msg, Nothing)
         Return msg
      End Function
      Public Function LogError( _
                  ByVal msg As String) _
                  As String
         ' Log the output and return the string to also log in the XML file
         mStatusOutput.AddStatusFailure(mXsltFileName, msg, Nothing)
         Return msg
      End Function
      Public Function LogInfo( _
                  ByVal msg As String) _
                  As String
         ' Log the output and return the string to also log in the XML file
         mStatusOutput.AddStatusInfo(mXsltFileName, VerboseLevel.Unknown, msg)
         Return msg
      End Function
#End Region
   End Class
End Namespace